﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LoLScrape;
using Ionic.Zip;
using Ionic.Zlib;

namespace LoLEdit
{
    public partial class frmMain : Form
    {
        private const string DEFAULT_LOL_FOLDER = "C:\\Riot Games\\League of Legends"; //"C:\\Program Files\\Riot Games\\League of Legends\\";
        private const string DB_CONNECT_STRING = "Data Source=D:\\Projects\\LoLBuilder\\LoLScrape\\LoLDB.sdf";

        private string m_LoLFolder;
        private LoLDB m_DB;

        public frmMain()
        {
            InitializeComponent();

            //Initialize connection to the LoLDB
            m_DB = new LoLDB(DB_CONNECT_STRING);

            //Set Selected Path to default setting
            m_LoLFolder = DEFAULT_LOL_FOLDER;

        } //end frmMain()

        #region Form Events

        /// <summary>
        /// Initializes the Form... (Hides all empty segment panels, etc)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {

        } //end frmMain_Load

        #endregion Form Events


        /// <summary>
        /// 
        /// </summary>
        private void OpenFile() 
        {
            frmOpenZipFile frm = new frmOpenZipFile();
            frm.ShowDialog();

            String strFilename = "DATA/Items/1018.inibin"; //"DATA/Characters/GeneralCharacterData.inibin";
            String strFileType = "Item";
            TabPage tab = null;
            ctlDocumentPage doc = null; 

            //Open the HeroPak
            ZipFile file = ZipFile.Read(m_LoLFolder + "\\game\\HeroPak_client.zip");

            ZipEntry entry = null;

            try
            {
                entry = file.Entries.Single(entryItem => entryItem.FileName == strFilename); 
            } //end try
            catch (System.ArgumentNullException)
            {


            } //end catch



            if (!tabMainTabControl.TabPages.ContainsKey(strFilename))
            {

            tabMainTabControl.TabPages.Add(strFilename, strFilename.Substring(strFilename.LastIndexOf('/')+1));
            tabMainTabControl.SelectTab(strFilename);
            int intIndex = tabMainTabControl.TabPages.IndexOfKey(strFilename);
            tab = tabMainTabControl.TabPages[intIndex];
            doc = new ctlDocumentPage();

            // 
            // ctlDocument
            // 
            doc.AutoSize = true;
            doc.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            doc.Dock = System.Windows.Forms.DockStyle.Top;
            doc.MinimumSize = new System.Drawing.Size(300, 0);
            // 
            // tabDoc1
            // 
            tab.AutoScroll = true;
            tab.UseVisualStyleBackColor = true;
            tab.Controls.Add(doc);

            //Scrape this inibin file
            CrcCalculatorStream stream = entry.OpenReader();

            //Scrape this inibin file
            Dictionary<UInt32, Object> dict = InibinReader.Scrape(stream);
            
            doc.ReportingProgress += new LoLEdit.ctlDocumentPage.ProgressEventHandler(LoadingClosingProgress);

            //Load the dictionary into the document page.
            doc.FileDictionary = dict;

            stsDocType.Text = strFileType;
            }






        } //end OpenFile

        private void SaveFile()
        {

            //byte[] junk = InibinGenerator.Generate(ctlDocument.FileDictionary);

            //Thread t = new Thread(new ThreadStart(MethodName));
            //t.SetApartmentState(ApartmentState.STA);
            //t.start()
            //while (t.IsAlive) {}
            
            
            //System.IO.FileStream fil = new System.IO.FileStream("C:/Junk.txt", FileMode.Create);
            //fil.Write(junk, 0, junk.Length);
            //fil.Flush();
            //fil.Close();

            //ZipFile file = ZipFile.Read("/game/HeroPak_client.zip");
            //if (file.ContainsEntry("filename")) file.RemoveEntry("filename");
            //file.AddEntry("Filename", bytes);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            OpenFile();
        } //end OpenFile

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SaveFile();
        }

        private void LoadingClosingProgress(object sender, LoLEdit.ctlDocumentPage.ProgressEventArgs e)
        {
            this.stsProgress.Value = e.Progress;
            this.stsStatusLabel.Text = e.Status;
            this.stsStatus.Refresh();
        }

    
    } //end frmMain

} //end LoLEdit
